﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsNotificationsTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// Job History Business Object
    /// </summary>

    public class Notifications : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected Notifications() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// NotificationID.
        /// </summary>
        public decimal NotificationID
        {
            get { return _notificationID; }
            set
            {
                if (_notificationID != value)
                {
                    if (OnPropertyChanging("NotificationID", _notificationID, value))
                    {
                        decimal oldValue = _notificationID;
                        _notificationID = value;
                        OnPropertyChanged("NotificationID", oldValue, value);
                    }
                }
            }
        }  private decimal _notificationID = -1;


        /// <summary>
        /// UserID.
        /// </summary>
        public int UserID
        {
            get { return _userID; }
            set
            {
                if (_userID != value)
                {
                    if (OnPropertyChanging("UserID", _userID, value))
                    {
                        int oldValue = _userID;
                        _userID = value;
                        OnPropertyChanged("UserID", oldValue, value);
                    }
                }
            }
        }  private int _userID = -1;

        /// <summary>
        /// ActionDone.
        /// </summary>
        public string ActionDone
        {
            get { return _actionDone; }
            set
            {
                if (_actionDone != value)
                {
                    if (OnPropertyChanging("ActionDone", _actionDone, value))
                    {
                        string oldValue = _actionDone;
                        _actionDone = value;
                        OnPropertyChanged("ActionDone", oldValue, value);
                    }
                }
            }
        }  private string _actionDone = null;


        /// <summary>
        /// Date/time at which the Notifications was created.
        /// </summary>
        public DateTime? NotificationDate
        {
            get { return _notificationDate; }
            set
            {
                if (_notificationDate != value)
                {
                    if (OnPropertyChanging("NotificationDate", _notificationDate, value))
                    {
                        DateTime? oldValue = _notificationDate;
                        _notificationDate = value;
                        OnPropertyChanged("NotificationDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _notificationDate = null;


        /// <summary>
        /// NotificationReference.
        /// </summary>
        public string NotificationReference
        {
            get { return _notificationReference; }
            set
            {
                if (_notificationReference != value)
                {
                    if (OnPropertyChanging("NotificationReference", _notificationReference, value))
                    {
                        string oldValue = _notificationReference;
                        _notificationReference = value;
                        OnPropertyChanged("NotificationReference", oldValue, value);
                    }
                }
            }
        }  private string _notificationReference = null;

        /// <summary>
        /// LinkedJobID.
        /// </summary>
        public string LinkedJobID
        {
            get { return _linkedJobID; }
            set
            {
                if (_linkedJobID != value)
                {
                    if (OnPropertyChanging("LinkedJobID", _linkedJobID, value))
                    {
                        string oldValue = _linkedJobID;
                        _linkedJobID = value;
                        OnPropertyChanged("LinkedJobID", oldValue, value);
                    }
                }
            }
        }  private string _linkedJobID = null;

        

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty Notifications object.
        /// </summary>
        public static Notifications CreateNew()
        {
            Notifications obj = new Notifications();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj._notificationDate = DateTime.Now;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static Notifications CreateNew(NotificationsCollection collection)
        {
            Notifications obj = CreateNew();

            // Get next/unique ID for the new object.
            decimal minVal = 0;
            foreach (Notifications notifications in collection)
            {
                if (notifications.NotificationID < minVal)
                    minVal = notifications.NotificationID;
            }
            obj.NotificationID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetCompleteNotifications

        /// <summary>
        /// Returns a collection of Notifications.
        /// </summary>
        public static NotificationsCollection GetCompleteNotifications()
        {

            NotificationsTableAdapter adapter = new NotificationsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsNotifications.NotificationsDataTable tbl = adapter.GetData();

            NotificationsCollection list = new NotificationsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsNotifications.NotificationsRow row in tbl.Rows)
                {
                    Notifications notifications = new Notifications();
                    RelationalToObject(notifications, row);
                    list.Add(notifications);
                }
            }

            return (list);
        }

        #endregion

        #region GetCompleteNotificationsByReferencse

        /// <summary>
        /// Returns a collection of Notifications.
        /// </summary>
        public static NotificationsCollection GetCompleteNotificationsByReferencse(string notificationreference)
        {

            NotificationsTableAdapter adapter = new NotificationsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsNotifications.NotificationsDataTable tbl = adapter.GetDataByReferenseItem(notificationreference);

            NotificationsCollection list = new NotificationsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsNotifications.NotificationsRow row in tbl.Rows)
                {
                    Notifications notifications = new Notifications();
                    RelationalToObject(notifications, row);
                    list.Add(notifications);
                }
            }

            return (list);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified Notifications.
        /// </summary>
        public static void Update(Notifications obj)
        {
            bool updateNotifications = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateNotifications = (obj.ObjState != ObjectState.Unchanged);

                if (!updateNotifications)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsNotifications.NotificationsDataTable tbl = null;
            if (updateNotifications)
            {

                NotificationsTableAdapter adapter = new NotificationsTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();


                tbl = new dsNotifications.NotificationsDataTable();
                dsNotifications.NotificationsRow row = tbl.NewNotificationsRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("NotificationsID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("Notifications: NotificationID={0}, UserID={1}, {2}", NotificationID, UserID));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more SubAdmin objects.
    /// </summary>

    public class NotificationsCollection : List<Notifications>
    {
        #region Types

        public class UserComparer : IComparer<Notifications>
        {
            public int Compare(Notifications n1, Notifications n2)
            {
                return (String.Compare(n1.NotificationID.ToString(), n2.NotificationID.ToString()));
            }
        }

        #endregion

        #region Public Methods

        #region FindByUserID

        public Notifications FindByUserID(decimal UserID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].UserID == UserID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion

    }
}
